local SkUtil = require "packages/water_jilve/utility/sk_util"
local sk__tiangong_creates = {}
local sk__tiangong_skills = {}
for loop = 1, 60, 1 do
  local m, n = loop % 10, loop % 12
  if m == 0 then
    m = 10
  end
  if n == 0 then
    n = 12
  end
  table.insert(sk__tiangong_skills, "sk__tiangong_create" .. m .. n)
  local sk__tiangong_create = fk.CreateSkill {

    name = "sk__tiangong_create" .. m .. n,

    tags = { Skill.Compulsory },

  }
  local event_list = { fk.EventPhaseStart, fk.EventPhaseEnd, fk.FinishJudge, fk.AfterCardsMove, fk.CardUseFinished,
    fk.TargetConfirmed, fk.TargetConfirming, fk.CardUsing, fk.CardResponding, fk.DamageCaused, fk.Damage, fk.DamageInflicted,
    fk.Damaged, fk.HpRecover, fk.MaxHpChanged, fk.HpLost, fk.EnterDying, fk.AfterDying, fk.EventAcquireSkill, fk.EventLoseSkill,
    fk.ChainStateChanged, fk.TurnedOver }
  for _, event1 in ipairs(event_list) do
    sk__tiangong_create:addEffect(event1, {
      anim_type = "special",
      can_trigger = function(self, event, target, player, data)
        local room = player.room
        if player:hasSkill(sk__tiangong_create.name) then
          if target then
            if target ~= player then return end
          end
          local info = player:getTableMark(sk__tiangong_create.name)[1]
          if info == 1 then
            return event == fk.EventPhaseStart and player.phase == Player.Start
          elseif info == 2 then
            return event == fk.EventPhaseStart and player.phase == Player.Judge
          elseif info == 3 then
            return event == fk.EventPhaseStart and player.phase == Player.Draw
          elseif info == 4 then
            return event == fk.EventPhaseStart and player.phase == Player.Play
          elseif info == 5 then
            return event == fk.EventPhaseEnd and player.phase == Player.Play
          elseif info == 6 then
            return event == fk.EventPhaseStart and player.phase == Player.Discard
          elseif info == 7 then
            return event == fk.EventPhaseEnd and player.phase == Player.Discard
          elseif info == 8 then
            return event == fk.EventPhaseStart and player.phase == Player.Finish
          else
            if player:usedSkillTimes(sk__tiangong_create.name, Player.HistoryTurn) >= (player:getTableMark(sk__tiangong_create.name)[4] or 0) then return end
            if info == 9 then
              return event == fk.FinishJudge
            elseif info == 10 then
              if event == fk.AfterCardsMove then
                for _, move in ipairs(data) do
                  if move.to == player and move.toArea == Player.Hand then
                    return true
                  end
                end
              end
            elseif info == 11 then
              return event == fk.CardUseFinished and data.card.type == Card.TypeBasic
            elseif info == 12 then
              return event == fk.CardUseFinished and data.card.type == Card.TypeTrick
            elseif info == 13 then
              return event == fk.CardUseFinished and data.card.type == Card.TypeEquip
            elseif info == 14 then
              return event == fk.CardUseFinished and data.card.color == Card.Red
            elseif info == 15 then
              return event == fk.CardUseFinished and data.card.color == Card.Black
            elseif info == 16 then
              return event == fk.TargetConfirmed and data.card:isCommonTrick()
            elseif info == 17 then
              if event == fk.AfterCardsMove then
                for _, move in ipairs(data) do
                  if move.from == player then
                    for _, move_info in ipairs(move.moveInfo) do
                      if move_info.fromArea == Card.PlayerEquip then
                        return true
                      end
                    end
                  end
                end
              end
            elseif info == 18 then
              return event == fk.TargetConfirming and data.card.trueName == "slash"
            elseif info == 19 then
              return event == fk.TargetConfirmed and data.card.trueName == "slash"
            elseif info == 20 then
              return (event == fk.CardUsing or event == fk.CardResponding) and data.card.trueName == "slash"
            elseif info == 21 then
              return (event == fk.CardUsing or event == fk.CardResponding) and data.card.trueName == "jink"
            elseif info == 22 then
              return event == fk.DamageCaused
            elseif info == 23 then
              return event == fk.Damage
            elseif info == 24 then
              return event == fk.DamageInflicted
            elseif info == 25 then
              return event == fk.Damaged
            elseif info == 26 then
              return event == fk.HpRecover or (event == fk.MaxHpChanged and data.num > 0)
            elseif info == 27 then
              return event == fk.HpLost or (event == fk.MaxHpChanged and data.num < 0)
            elseif info == 28 then
              return event == fk.EnterDying
            elseif info == 29 then
              return event == fk.AfterDying
            elseif info == 30 then
              return event == fk.EventAcquireSkill
            elseif info == 31 then
              return event == fk.EventLoseSkill and data.skill:isPlayerSkill(player) and data.skill.visible
            elseif info == 32 then
              return event == fk.ChainStateChanged or event == fk.TurnedOver
            end
          end
        end
      end,
      on_use = function(self, event, target, player, data)
        local room = player.room
        local targets = {}
        switch(player:getTableMark(sk__tiangong_create.name)[2], {
          [1] = function()
            targets = { player }
          end,
          [2] = function()
            targets = room:getAlivePlayers()
          end,
          [3] = function()
            targets = room:getOtherPlayers(player)
          end,
          [4] = function()
            targets = table.filter(room:getAlivePlayers(), function(p)
              return p.gender == General.Male or p.gender == General.Bigender
            end)
          end,
          [5] = function()
            targets = table.filter(room:getAlivePlayers(), function(p)
              return p.gender == General.Female or p.gender == General.Bigender
            end)
          end,
          [6] = function()
            targets = table.filter(room:getAlivePlayers(), function(p)
              return p.kingdom == "wei"
            end)
          end,
          [7] = function()
            targets = table.filter(room:getAlivePlayers(), function(p)
              return p.kingdom == "shu"
            end)
          end,
          [8] = function()
            targets = table.filter(room:getAlivePlayers(), function(p)
              return p.kingdom == "wu"
            end)
          end,
          [9] = function()
            targets = table.filter(room:getAlivePlayers(), function(p)
              return p.kingdom == "qun"
            end)
          end,
          [10] = function()
            targets = table.filter(room:getAlivePlayers(), function(p)
              return not table.contains({ "wei", "shu", "wu", "qun" }, p.kingdom)
            end)
          end,
          [11] = function()
            targets = table.random(room:getAlivePlayers(), 1)
          end,
          [12] = function()
            targets = table.random(room:getAlivePlayers(), 2)
          end,
          [13] = function()
            targets = table.random(room:getAlivePlayers(), 3)
          end,
          [14] = function()
            targets = table.random(room:getAlivePlayers(), math.random(#room.alive_players))
          end,
          [15] = function()
            targets = table.filter(room:getAlivePlayers(), function(a)
              return table.every(room.alive_players, function(b)
                return a.maxHp >= b.maxHp
              end)
            end)
          end,
          [16] = function()
            targets = table.filter(room:getAlivePlayers(), function(a)
              return table.every(room.alive_players, function(b)
                return a.maxHp <= b.maxHp
              end)
            end)
          end,
          [17] = function()
            targets = table.filter(room:getAlivePlayers(), function(a)
              return table.every(room.alive_players, function(b)
                return a.hp >= b.hp
              end)
            end)
          end,
          [18] = function()
            targets = table.filter(room:getAlivePlayers(), function(a)
              return table.every(room.alive_players, function(b)
                return a.hp <= b.hp
              end)
            end)
          end,
          [19] = function()
            targets = table.filter(room:getAlivePlayers(), function(a)
              return table.every(room.alive_players, function(b)
                return a:getHandcardNum() >= b:getHandcardNum()
              end)
            end)
          end,
          [20] = function()
            targets = table.filter(room:getAlivePlayers(), function(a)
              return table.every(room.alive_players, function(b)
                return a:getHandcardNum() <= b:getHandcardNum()
              end)
            end)
          end,
          [21] = function()
            targets = table.filter(room:getAlivePlayers(), function(p)
              return not p:isWounded()
            end)
            if #targets == 0 then return end
            targets = table.random(targets, 1)
          end,
          [22] = function()
            targets = table.filter(room:getAlivePlayers(), function(p)
              return not p:isWounded()
            end)
            if #targets == 0 then return end
            targets = table.random(targets, 2)
          end,
          [23] = function()
            targets = table.filter(room:getAlivePlayers(), function(p)
              return not p:isWounded()
            end)
          end,
          [24] = function()
            targets = table.filter(room:getAlivePlayers(), function(p)
              return p:isWounded()
            end)
            if #targets == 0 then return end
            targets = table.random(targets, 1)
          end,
          [25] = function()
            targets = table.filter(room:getAlivePlayers(), function(p)
              return p:isWounded()
            end)
            if #targets == 0 then return end
            targets = table.random(targets, 2)
          end,
          [26] = function()
            targets = table.filter(room:getAlivePlayers(), function(p)
              return p:isWounded()
            end)
          end,
        })

        if #targets == 0 then return end
        room:doIndicate(player.id, table.map(targets, Util.IdMapper))

        local info = player:getTableMark(sk__tiangong_create.name)[3]
        for _, p in ipairs(targets) do
          if not p.dead then
            room:delay(500)
            SkUtil.DoTiangongEffect(player, p, info, sk__tiangong_create.name)
          end
        end
      end,
    })
  end
  local dic = {
    { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" },
    { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }
  }
  Fk:loadTranslationTable {
    ["sk__tiangong_create" .. m .. n] = dic[1][m] .. dic[2][n],
    [":sk__tiangong_create" .. m .. n] = "锁定技，某时机，某些角色执行某些效果。（具体描述点击“机关技能”标记查看）",
  }
  table.insert(sk__tiangong_creates, sk__tiangong_create)
end
Fk:addQmlMark {
  name = "sk__tiangong_create",
  how_to_show = function(name, value)
    if type(value) == "table" then
      return tostring(#value)
    end
    return " "
  end,
  qml_path = "packages/water_jilve/qml/TiangongDetailBox"
}
return sk__tiangong_creates
